
import ProTable from '@ant-design/pro-table';
import type { ProColumns } from '@ant-design/pro-table';
import { Modal } from 'antd';
import React, { useState } from 'react';
import {ProductType,NormalListParam} from '../../model';
import request from '@/utils/request';
import { manufacturerOption,manufacturerOptionOnly } from '@/utils/utils';
  
export type ChooseProps = {
    onCancel: (flag?: boolean) => void;
    onSubmit: (values: ProductType[]) => Promise<void>;
    modalVisible: boolean;
    values: any[];
    wh?:[];
    apiType?:number;
    status?:number;
    defaultWH?:string;
  };

  
const WHProductChoose: React.FC<ChooseProps> = (props)=>{

    async function getMyAllProduct(params: NormalListParam) {
      var data =  request('/xwyl/take/getMyAllProduct', {
        params,
      });
      return data;
    }

    async function getMyAllProduct2(params: NormalListParam) {
      var data =  request('/xwyl/take/getMyAllProduct2', {
        params,
      });
      return data;
    }
    
    const [selectedRowsState, setSelectedRows] = useState<ProductType[]>();

    const rowSelection = {
      onChange: (key:any, selectedRows:any) => {
        setSelectedRows(selectedRows);
      },
      getCheckboxProps: (record:any) => {
        var index = props.values.findIndex((item)=>{
          return item.id === record.id;
        });
        if(index<0){
          return {
            disabled: false,
            name: record.name,
          }
        }else{
          return {
            disabled: true,
            name: record.name,
          }
        }
      }
    };

    const whRequest=async()=>{
      return props.wh;
    }
    
    const columns: ProColumns<ProductType>[] = [
        {
          title: '厂家',
          dataIndex: 'manufacturer',
          valueType:'select',
          hideInSearch:true,
          valueEnum:manufacturerOption(),
          width:80,
        },
        {
          title: '厂家',
          dataIndex: 'manufacturer',
          valueType:'select',
          hideInTable:true,
          valueEnum:manufacturerOptionOnly(),
          width:80,
        },
        {
          title:'产品名称',
          ellipsis:true,
          dataIndex: 'product_name',
        },
        {
          ellipsis:true,
          title:'产品型号',
          dataIndex: 'product_standard',
        },
        {
          title: '单位',
          dataIndex: 'packing_unit',
          width:60,
          ellipsis:true,
          hideInSearch:true
        },
        // {
        //   title:'库位',
        //   dataIndex: 'warehouseId',
        //   valueType:'select', 
        //   request:whRequest,
        //   hideInSearch:props.defaultWH!=''?true:false,
        //   hideInTable:true,
        // },
        {
          title:'库位',
          dataIndex: 'warehouseName',
          width:120,
          ellipsis:true,
        },
        {
          title:'批号',
          dataIndex: 'batch',
          width:120,
          ellipsis:true,
        },
        {
          title:'失效日期',
          dataIndex: 'validity',
          hideInSearch:true,
          width:120,
          ellipsis:true,
        },
        {
          title:'数量',
          dataIndex: 'numbs',
          hideInSearch:true,
        },
      ];

    return (
      <Modal
        title="选择产品"
        width={1100}
        destroyOnClose={true}
        visible={props.modalVisible}
        onOk={()=>{
            var data = selectedRowsState.filter((item:any)=>{       
              return !item.choose;
            });
            props.onSubmit(data);
            setSelectedRows([]);
        }}
        onCancel={()=>{
            props.onCancel();
        }}
      >
        <ProTable<ProductType>
          headerTitle='产品列表'
          scroll={{ y: 300 }}
          toolBarRender={false}
          request={async(params, sorter, filter) =>{
            params.status = props.status;
            if(props.defaultWH!=''){
              const wh={'warehouseId':props.defaultWH};
              if(props.apiType==1){
                return getMyAllProduct2({...params,filter,...wh})
              }else{
                return getMyAllProduct({...params,filter,...wh})
              }
            }
            if(props.apiType==1){
              return getMyAllProduct2({...params,filter})
            }else{
              return getMyAllProduct({...params,filter})
            }
          }}
          columns={columns}
          rowKey="id"
          pagination={{pageSizeOptions: ['20', '100', '500']}}
          search={{
            filterType: 'query',
            layout: 'horizontal',
        }}
            rowSelection={{
              type: "checkbox",
              ...rowSelection,
            }}
          />
      </Modal>
    );
}
export default WHProductChoose;